#include "Drawing.h"

Drawing::~Drawing()
{
//	if(img != NULL)
//		oslDebug("img not null");
}
Drawing::Drawing()
{
	type = d_nothing;
	x = -1; y = -1;
	x2 = -1; y2 = -1;
	color = Color(0,0,0);
	visible = false;
	img = NULL;
	font = NULL;
	PF = OSL_PF_5551;
	fontwasloaded = false;
} 
//FilledRect & FramedRect & Line
Drawing::Drawing(int _x, int _y, int _x2, int _y2, Color c, int _type)
{
	type = _type;
	x = _x; y = _y;
	x2 = _x2; y2 = _y2;
	color = c;
	visible = true;
	img = NULL;
	font = NULL;
	PF = OSL_PF_5551;
	fontwasloaded = false;
}
Drawing::Drawing(int _x, int _y, int _x2, int _y2, int _type)
{
	type = _type;
	x = _x; y = _y;
	x2 = _x2; y2 = _y2;
	color = Color(0,0,0);
	visible = true;
	PF = OSL_PF_5551;
	img = NULL;
	font = NULL;
	fontwasloaded = false;
}
//Textbox & Text & Image
Drawing::Drawing(string srctext,int _x, int _y, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = -1; y2 = -1;
	color = Color(255,255,255);
	bgcolor = Color(0,0,0,0);
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = OSL_PF_5551;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext,int _x, int _y, Color c, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = -1; y2 = -1;
	color = c;
	bgcolor = Color(0,0,0,0);
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = OSL_PF_5551;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext,int _x, int _y, Color c, Color bg,int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = -1; y2 = -1;
	color = c;
	bgcolor = bg;
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = OSL_PF_5551;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext, int _x, int _y, int _w, int _h, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = x + _w; y2 = y + _h;
	color = Color(255,255,255);
	bgcolor = Color(0,0,0,0);
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = OSL_PF_5551;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext, int _x, int _y, int _w, int _h, Color c, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = x + _w; y2 = y + _h;
	color = c;
	bgcolor = Color(0,0,0,0);
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = OSL_PF_5551;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext, int _x, int _y, int _w, int _h, Color c, Color bg, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = x + _w; y2 = y + _h;
	color = c;
	bgcolor = bg;
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = OSL_PF_5551;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext, int _x, int _y, OSL_PIXELFORMAT _PF, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = -1; y2 = -1;
	color = Color();
	bgcolor = Color();
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = _PF;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
Drawing::Drawing(string srctext, int _x, int _y, int _w, int _h, OSL_PIXELFORMAT _PF, int _type)
{
	type = _type;
	textdata = srctext;
	x = _x; y = _y;
	x2 = x + _w; y2 = y + _h;
	color = Color();
	bgcolor = Color();
	img = NULL;
	font = NULL;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	PF = _PF;
	if(type == d_image)
		loadImage();
	visible = true;
	fontwasloaded = false;
}
void Drawing::loadImage()
{
	if(img != NULL)
	{
		oslDeleteImage(img);
		img = NULL;
	}
	img = oslLoadImageFile((char *)textdata.c_str(),OSL_IN_RAM,PF);
	if(img)
	{
		img->x = x; img->y = y;
		if(x2 > 0 && y2 > 0)
		{
			img->stretchX = x2 - x;
			img->stretchY = y2 - y;
		}
		else
		{
			x2 = x + img->stretchX;
			y2 = y + img->stretchY;
		}
	}
}
void Drawing::drawImage()
{
	if(img != NULL)
		oslDrawImage(img);
}
void Drawing::drawString()
{
	OSL_FONT *prevfont = osl_curFont;
	oslSetTextColor(color.osl_c());
	oslSetBkColor(bgcolor.osl_c());
	if(font != NULL)
	{
		if(font->fontType == OSL_FONT_INTRA)
			oslIntraFontSetStyle(font,fsize,color.osl_c(),bgcolor.osl_c(),foptions);
		oslSetFont(font);
	}
	oslDrawString(x,y,textdata.c_str());
	oslSetFont(prevfont);
}
void Drawing::drawTextBox()
{
	OSL_FONT *prevfont = osl_curFont;
	oslSetTextColor(color.osl_c());
	oslSetBkColor(bgcolor.osl_c());
	if(font != NULL)
	{
		if(font->fontType == OSL_FONT_INTRA)
			oslIntraFontSetStyle(font,fsize,color.osl_c(),bgcolor.osl_c(),foptions);
		oslSetFont(font);
	}
	oslDrawTextBox(x,y,x2,y2,textdata.c_str(),0);
	oslSetFont(prevfont);
}
void Drawing::redraw()
{
	if(visible) switch(type)
	{
		case d_nothing: 	break;
		case d_filledrect: 	oslDrawFillRect(x,y,x2,y2,color.osl_c());
							break;
		case d_framedrect: 	oslDrawRect(x,y,x2,y2,color.osl_c());
							break;
		case d_line:		oslDrawLine(x,y,x2,y2,color.osl_c());
							break;
		case d_image:		drawImage();
							break;
		case d_text:		drawString();
							break;
		case d_textbox:		drawTextBox();
							break;				
		default: 			break;
							
	}
}
void Drawing::setFont(OSL_FONT *_font)
{
	font = _font;
	fontwasloaded = false;
}
bool Drawing::setFont(string src)
{
	if(fontwasloaded)
		oslDeleteFont(font);
	font = oslLoadFontFile(src.c_str());
	fontwasloaded = true;
	if(!font)
		return false;
	return true;
}
void Drawing::setImage(OSL_IMAGE *_img)
{
	oslCopyImageTo(img,_img);
}
void Drawing::setColor(Color c)
{
	color = c;
}
void Drawing::setBgColor(Color bg)
{
	bgcolor = bg;
}
void Drawing::setText(string text)
{
	textdata = text;
}
void Drawing::setSrc(string src)
{
	textdata = src;
	loadImage();
}
void Drawing::setX(int _x)
{
	int w = getWidth();
	x = _x;
	x2 = x + w;
}
void Drawing::setY(int _y)
{
	int h = getHeight();
	y = _y;
	y2 = y + h;
}
void Drawing::moveTo(int _x, int _y)
{
	setX(_x); setY(_y);
}
void Drawing::move(int dx, int dy)
{
	x += dx; y += dy;
	x2 += dx; y2 += dy;
}
void Drawing::setStart(int _x1, int _y1)
{
	x = _x1; y = _y1;
}
void Drawing::setEnd(int _x2, int _y2)
{
	x2 = _x2; y2 = _y2;
}
void Drawing::setWidth(int _w)
{
	x2 = x + _w;
}
void Drawing::setHeight(int _h)
{
	y2 = y + _h;
}
void Drawing::setSize(Size s)
{
	x2 = x + s.w;
	y2 = y + s.h;
}
void Drawing::setOptions(unsigned short int _foptions)
{
	foptions = _foptions;
}
void Drawing::setFontSize(float size)
{
	fsize = size;
}
void Drawing::hide()
{
	visible = false;
}
void Drawing::show()
{
	visible = true;
}
bool Drawing::toggleVisibility()
{
	visible = !visible;
	return visible;
}
void Drawing::setVisibility(bool vis)
{
	visible = vis;
}
OSL_IMAGE * Drawing::getImage()
{
	return img;
}
OSL_FONT * Drawing::getFont()
{
	return font;
}
float Drawing::getFontSize()
{
	return fsize;
}
Color Drawing::getColor()
{
	return color;
}
Color Drawing::getBgColor()
{
	return bgcolor;
}
string Drawing::getText()
{
	return textdata;
}
string Drawing::getSrc()
{
	return textdata;
}
int Drawing::getX()
{
	return x;
}
int Drawing::getY()
{
	return y;
}
Location Drawing::getLocation()
{
	return Location(x,y);
}
int Drawing::getWidth()
{
	unsigned int w = x2 - x;
	return w;
}
int Drawing::getHeight()
{
	unsigned int h = y2 - y;
	return h;
}
Location Drawing::getStart()
{
	return Location(x,y);
}
Location Drawing::getEnd()
{
	return Location(x2,y2);
}
Size Drawing::getSize()
{
	return Size(getWidth(),getHeight());
}
unsigned short int Drawing::getOptions()
{
	return foptions;
}
bool Drawing::isVisible()
{
	return visible;
}
bool Drawing::imageIsLoaded()
{
	if(img)
		return true;
	return false;
}
void Drawing::uncache()
{
//	TODO uncache
//	font was loaded and others
	if(fontwasloaded)
		oslDeleteFont(font);
	if(img != NULL)
	{
		oslDeleteImage(img);
		img = NULL;
	}
}
